<template>
    <view>
        <view class="padding text-left">
            <button @click="agree" class="cu-btn radius shadow bg-green margin-right">审核通过</button>
            <button @click="disagree" class="cu-btn radius shadow bg-red right margin-left">审核不通过</button>
        </view>
        <view class="uni-container padding">
            <uni-table
                ref="table"
                :loading="loading"
                border
                stripe
                type="selection"
                emptyText="暂无更多数据"
                @selection-change="selectionChange">
                <uni-tr>
                    <uni-th width="30" align="center">学号</uni-th>
                    <uni-th align="center">姓名</uni-th>
                    <uni-th align="center">学院</uni-th>
                </uni-tr>
                <uni-tr v-for="(item, index) in tableData" :key="index">
                    <uni-td>{{ item.sid }}</uni-td>
                    <uni-td>
                        <view class="name">{{ item.name }}</view>
                    </uni-td>
                    <uni-td align="center">{{ item.college }}</uni-td>

                </uni-tr>
            </uni-table>
            <view class="uni-pagination-box"><uni-pagination show-icon :page-size="pageSize" :current="pageCurrent" :total="total" @change="change" /></view>
        </view>
    </view>
</template>

<script>

const app = getApp();

export default {
    data() {
        return {
            id: 1,
            tableData: [{
                college:"",
                name:"",
                sid:"",
                association_id:-1,
                user_id:-1,
                join_time:null,
                apply_time:"",
                authority:-1,
                active:-2,
                department_id:null
            }],
            // 每页数据量
            pageSize: 10,
            // 当前页
            pageCurrent: 1,
            // 数据总量
            total: 0,
            loading: false,
            selectedIndexs: []
        }
    },
    onPullDownRefresh() {
        this.getApplyList(this.id);
        uni.stopPullDownRefresh();
    },
    onLoad(e) {
        // this.selectedIndexs = []
        // this.getData(1)
        this.id = e.id
        this.getApplyList(e.id)
    },
    onBackPress() {
        uni.reLaunch({
            url: '../../pages/association/detail/detail?id=' + this.id
        });
    },
    methods: {

        /**
         * 审核通过
         */
        agree() {
            let that = this;
            let selectedItems = that.selectedItems();

            if (selectedItems.length === 0) {
                uni.showToast({
                    title: '请选择要审核的用户',
                    icon: 'none'
                });
                return;
            }

            // 审核通过
            that.$myFunction.getData(app.globalData.ApiUrl+"/api/Android/agreeJoinAssociation",{
                association_id: that.id,
                SafeCode: uni.getStorageSync('SafeCode'),
                agreeItems: selectedItems
            }).then(res => {

                if(res.code == 1){
                    uni.showToast({
                        title: '审核通过',
                        icon: 'none'
                    });
                }else {
                    uni.showToast({
                        title: res.msg,
                        icon: 'none'
                    });
                }

            });

            that.getApplyList(that.id);

        },
        /**
         * 审核不通过
         **/
        disagree() {
            let that = this;
            let selectedItems = that.selectedItems();

            if (selectedItems.length === 0) {
                uni.showToast({
                    title: '请选择要审核的用户',
                    icon: 'none'
                });
                return;
            }

            // 审核不通过
            that.$myFunction.getData(app.globalData.ApiUrl+"/api/Android/disagreeJoinAssociation",{
                association_id: that.id,
                SafeCode: uni.getStorageSync('SafeCode'),
                disagreeItems: selectedItems
            }).then(res => {

                if(res.code == 1){
                    uni.showToast({
                        title: '审核完成',
                        icon: 'none'
                    });
                } else {
                    uni.showToast({
                        title: res.msg,
                        icon: 'none'
                    });
                }
            });

            that.getApplyList(that.id);
        },

        // 获取数据
        getApplyList(id) {
            console.log(id)
            this.loading = true
            this.$myFunction.getData(app.globalData.ApiUrl+'/api/Android/getApplyList', {
                association_id: id,
                SafeCode: uni.getStorageSync('SafeCode')
            }).then(res => {
                this.loading = false
                console.log(res)
                if (res.code == 1) {
                    this.tableData = res.data
                }
            });
        },

        // 多选处理
        selectedItems() {
            return this.selectedIndexs.map(i => this.tableData[i])
        },
        // 多选
        selectionChange(e) {
            console.log(e.detail.index)
            this.selectedIndexs = e.detail.index
        },
        // 分页触发
        change(e) {
            this.$refs.table.clearSelection()
            this.selectedIndexs.length = 0
            this.getData(e.current)
        }
    }
}
</script>

<style>

</style>
